package com.pansoft.openplanet.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import androidx.core.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.efounder.constant.EnvironmentVariable;
import com.efounder.utils.ResStringUtil;
import com.pansoft.openplanet.R;
import com.pansoft.openplanet.bean.AirDropDetail;
import com.pansoft.openplanet.util.TCRequestUtil;
import com.utilcode.util.ToastUtils;

import org.json.JSONObject;

import java.util.HashMap;

/**
 * 领取空投界面
 */
public class AirDropTaskResultActivity extends TCBaseActivity {

    private static final String TAG = "AirDropTaskResultActivity";
    private AnimationDrawable animationDrawable;
    private ImageView transferAnim;
    private TextView tvTransferStatus;
    private TextView tvHint;
    AirDropDetail.AirportBean airportBean;

    public static void start(Context context, String address, AirDropDetail.AirportBean airportBean) {
        Intent starter = new Intent(context, AirDropTaskResultActivity.class);
        starter.putExtra("address", address);
        starter.putExtra("airportBean", airportBean);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.open_planet_activity_air_drop_result);
        initView();
        airportBean = (AirDropDetail.AirportBean) getIntent().getSerializableExtra("airportBean");
        requestData();
    }


    private void initView() {

        TextView tvTitle = (TextView) findViewById(R.id.tv_title);
        tvTitle.setText(ResStringUtil.getString(R.string.open_planet_airdrop_detail));
        transferAnim = (ImageView) findViewById(R.id.transfer_anim);
        tvTransferStatus = (TextView) findViewById(R.id.tv_transfer_status);
        tvHint = (TextView) findViewById(R.id.tv_hint);
        animationDrawable = (AnimationDrawable) transferAnim.getBackground();
        animationDrawable.start();

        tvHint.setVisibility(View.GONE);
        tvTransferStatus.setText(ResStringUtil.getString(R.string.open_planet_airdropping));

    }

    private void requestData() {
        //http://192.168.2.105:8080/tcserver/aridrop/grabAirdrop?address=0x8AaBc6B32Ae3EAE165A5760b5CAf0507Afea1a5a
        // &userId=18515696994&coinName=test4&airdropId=10&organization=ddd&picture=a&type=0
        HashMap<String, String> map = new HashMap<>();
        map.put("airdropId", airportBean.getId() + "");
        map.put("userId", EnvironmentVariable.getUserName());
        map.put("picture", airportBean.getAirportPicture() + "");
        map.put("organization", airportBean.getOrganization() + "");
        map.put("coinName", airportBean.getCoinName() + "");
        map.put("address", getIntent().getStringExtra("address") + "");
        TCRequestUtil.getCommonRequest(TAG, "aridrop/grabAirdrop", map, new TCRequestUtil.TCRequestCallback() {
            @Override
            public void onSuccess(String response) {
                try {
                    final JSONObject jsonObject = new JSONObject(response);

                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            //{"result":"success","data":7.04175}
                            if ("success".equals(jsonObject.optString("result"))) {
                                stopAnimation(ResStringUtil.getString(R.string.open_planet_airdrop_success), true);
                                tvHint.setText(getString(R.string.open_planet_airdrop_had_hint, jsonObject.optString("data"),airportBean.getCoinName()));
                            } else {

                                //{"result":"fail","data":"have token"}
                                if (jsonObject.optString("data", "").equals("have token")) {
                                    stopAnimation(ResStringUtil.getString(R.string.open_planet_airdrop_fail), false);
                                    tvHint.setVisibility(View.VISIBLE);
                                    tvHint.setText(ResStringUtil.getString(R.string.open_planet_airdrop_had_and_check));
                                } else {
                                    stopAnimation(ResStringUtil.getString(R.string.open_planet_airdrop_fail), false);

                                }


                            }
                        }
                    },3000);

                } catch (Exception e) {
                    e.printStackTrace();
                    stopAnimation(ResStringUtil.getString(R.string.open_planet_airdrop_fail), false);
                    ToastUtils.showShort(R.string.open_planet_airdrop_fail);
                }

            }

            @Override
            public void onFail(String error) {
                stopAnimation(ResStringUtil.getString(R.string.open_planet_airdrop_fail), false);
                ToastUtils.showShort(R.string.common_text_http_request_fail);

            }
        });


    }

    private void stopAnimation(String text, boolean isSuccess) {
        animationDrawable.stop();
        transferAnim.setBackground(null);
        if (isSuccess) {
            transferAnim.setImageDrawable(ContextCompat
                    .getDrawable(AirDropTaskResultActivity.this, R.drawable.transfer_success));
            tvHint.setVisibility(View.VISIBLE);
        } else {
            transferAnim.setImageDrawable(ContextCompat
                    .getDrawable(AirDropTaskResultActivity.this, R.drawable.transfer_fail));
        }
        tvTransferStatus.setText(text);

    }
}
